var AbstractModule = Class.create({

	// set this to true to open the calculation result in a new scene instead of a dialog
	openResultInScene: false,
	help: false,

	initialize: function(title) {
		this.title = title;
		$("header").innerHTML = title;
	},

	setup: function() {
		this.setupWidgets();

		this.controller.setupWidget("calcButton", {}, {
			buttonLabel: $("calcButton").innerHTML || "Calculate"
		});
		this.controller.listen("calcButton", Mojo.Event.tap, this.calc.bindAsEventListener(this));

		var items = [];
		items.push({
			label: "Reference",
			command: "ref"
		});
		items.push({});

		if (this.help) {
			items.push({
				label: "Help",
				command: "help"
			});
		}
		this.controller.setupWidget(Mojo.Menu.commandMenu, {}, {
			items: items
		});
	},

	handleCommand: function(event) {
		if (event.type == Mojo.Event.command) {
			switch (event.command) {
				case "help":
					this.showDialog("Help", help());
					break;
				case "ref":
					this.showDialog("Reference", ref());
					break;
			}
		}
	},

	setupWidgets: function() {
		// please override, setup widgets and listeners here
	},

	calculate: function() {
		// please override and return string representation of the calculation result
		return null;
	},

	calc: function() {
		var result = this.calculate();
		if (result) {
			if (this.openResultInScene) {
				this.controller.stageController.pushScene("result", this.title, result);
			} else {
				this.showDialog(this.title, result);
			}
		}
	},

	showDialog: function(title, message) {
		this.controller.showAlertDialog({
			onChoose: function() {
			},
			title: title,
			message: message,
			choices:[{label: "OK", value:""}]
		});
	}
});